package com.base.cn.platform.os.controller.user.member;

import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.mybatis.Pagination;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.user.member.CusUserMemberBuyRecord;
import com.base.cn.platform.os.service.user.member.CusUserMemberBuyRecordBiz;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Map;


/**
 * @Data 用户会员购买记录
 * @ClassName  CusUserMemberBuyRecordController
 * @author :ll
 * @Create Date : 2018年4月26日
 */
@RestController
@RequestMapping("/manage/user")
public class CusUserMemberBuyRecordController extends BaseController {

    @Autowired
    private CusUserMemberBuyRecordBiz cusUserMemberBuyRecordBiz;

    /**
     * 检测用户是否购买过会员
     * @param userId 用户ID
     * @return true购买过，false未购买过
     */
    @RequestMapping("/checkUserBuyMember")
    public boolean checkUserBuyMember(@RequestParam("userId") BigDecimal userId){
        return cusUserMemberBuyRecordBiz.checkUserBuyMember(userId);
    }

    /**
     * 修改会员开通记录状态
     * @param id 开通记录ID
     * @param status 状态，1正常，2禁用
     * @return Map<String,Object>
     */
    @RequestMapping("/updateUserMemberBuyRecordStatus")
    public Map<String,Object> updateUserMemberBuyRecordStatus( @RequestParam("id") BigDecimal id,
                                                              @RequestParam("status") Integer status){
            if(!DataUtil.idIsNotNull(id)){
                return ResultUtil.ERROR("数据错误，操作失败");
            }
            if(status!=1 && status!=2){
                return  ResultUtil.ERROR("状态错误，操作失败");
            }
            CusUserMemberBuyRecord memberBuyRecord = new CusUserMemberBuyRecord();
            memberBuyRecord.setId(id);
            memberBuyRecord.setStatus(status);
            cusUserMemberBuyRecordBiz.updateById(memberBuyRecord,null);
            String message ="";
            if(status==1){
                message="恢复成功";
            }else if(status==2){
                message="禁用成功";
            }
            return ResultUtil.SUCCESS(message,memberBuyRecord);
    }

    /**
     * 检测用户是否有会员权限
     * @param userId 用户ID
     * @return true有，false 没有
     */
    @RequestMapping("/checkUserUseMemberPrice")
    public boolean checkUserUseMemberPrice(@RequestParam("userId") BigDecimal userId){
        if(!DataUtil.idIsNotNull(userId)){
            return false;
        }
        return cusUserMemberBuyRecordBiz.checkUserUseMemberPrice(userId);
    }

    /**
     * 添加会员购买与你
     * @param userId 用户ID
     * @param day 购买会员天数
     */
    @RequestMapping("/addUserMemberBuyRecord")
    public void addUserMemberBuyRecord(@RequestParam("userId") BigDecimal userId,@RequestParam("day") int day){
        if(DataUtil.idIsNotNull(userId) && day>0){
            cusUserMemberBuyRecordBiz.addUserMemberBuyRecord(userId,day);
        }
    }

    /**
     * 修改会员有效时间 (用于退费)
     * @param userId 用户ID
     * @param day 更新天数（正数是加，负数是减）
     */
    @RequestMapping("/updateMemberBuyRecordValidTime")
    public void updateMemberBuyRecordValidTime(@RequestParam("userId") BigDecimal userId,@RequestParam("day") int day){
        if(DataUtil.idIsNotNull(userId) && day!=0){
            cusUserMemberBuyRecordBiz.updateMemberBuyRecordValidTime(userId,day);
        }
    }

    /**
     * 获取用户开通的会员记录
     * @param userId 用户ID
     * @return UserMemberBuyRecord
     */
    @RequestMapping("/queryUserBuyMemberRecord")
    public CusUserMemberBuyRecord queryUserBuyMemberRecord(@RequestParam("userId") BigDecimal userId){
        return cusUserMemberBuyRecordBiz.queryUserBuyMemberRecord(userId);
    }


    /**
     * 搜索分页用户会员记录
     * @param userMemberBuyRecord
     * @param currentPage
     * @return
     */
    @RequestMapping("/queryUserMemberBuyRecordListPage")
    public PageInfo<CusUserMemberBuyRecord> queryUserMemberBuyRecordListPage(
                                                     @RequestBody CusUserMemberBuyRecord userMemberBuyRecord,
                                                     @RequestParam(value = "currentPage",required = false,defaultValue = "1") Integer currentPage){
        Pagination page = new Pagination();
        page.setCurrentPage(currentPage);
        return cusUserMemberBuyRecordBiz.queryUserMemberBuyRecordList(userMemberBuyRecord,page,true);
    }


}
